package edu.pku.sei.mte.transformation.qvt;

import edu.pku.sei.mte.constants.SearchResult;
import edu.pku.sei.mte.mtemodel.mtmodel.rulebased.RBTransformation;
import edu.pku.sei.mte.mtemodel.mtmodel.rulebased.Relation;
import edu.pku.sei.mte.mtemodel.mtmodel.rulebased.Rule;
import edu.pku.sei.mte.runtime.rulebased.RBTContext;
import edu.pku.sei.mte.runtime.rulebased.RBTEnvironment;

public class QVTCheckOnlyVisitor extends QVTSemanticVisitor {

	public QVTCheckOnlyVisitor(RBTEnvironment env) {
		super(env);
	}
	@Override
	public Object visitRBTransformation(RBTransformation e) throws Exception {
		for(Rule r : e.getTopLevelRules()){
			RBTContext c = this.getEnv().createNewContext();
			r.registVariableToContext(c);
			getContextStack().push(c);
			System.out.println("Check Rule "+r.getName());
			if(r.accept(this) != Boolean.TRUE){
				System.out.println("Rule " + r.getName() + " failed");
				return Boolean.FALSE;
			}
			System.out.println("Rule " + r.getName() + " passed");
			getContextStack().pop();
		}
		return Boolean.TRUE;
	}
	@Override
	public Object visitRelation(Relation e) throws Exception {
		return getSemantic().findContext(e, getContextStack().peek(), null, null, false) == SearchResult.ALL_SATISFIED;
	}
}
